$('document').ready(e => {

    $("#begger").modal("show")

    // 监听输入框中的内容, 当发现内容改变时, 进行格式化操作
    //$('.original-input').bind('input propertychange', function (e) {
        //var originalInput = $('.original-input').val();
        //originalInput = originalInput.replace(/[\r\n]/g," ")
        //$('.original-input').val(originalInput);
        //console.log(originalInput)
    //});

    // 设置 点击翻译按钮进行翻译
    $('#translate').click(function (e) { 
        translate();
    });

    // 点击格式化输入, 对输入进行格式化
    $('#format').click(function (e) { 
        format();
    });

    //自动格式化监听
    $('.original-input').bind('input propertychange', function (e) {
        if ($("#autoformat")[0].checked) {
            //自动格式化
            var originalInput = $('.original-input').val();
            originalInput = originalInput.replace(/[\r\n]/g," ")
            $('.original-input').val(originalInput);
        }

        if ($("#autotranslate")[0].checked) {
            //自动格式化
            translate();
        }
    });

    // 更换背景
    $('#bg1').click(function (e) { 
        $('body').css('background','url(image/bg.jpeg)');
    });
    $('#bg2').click(function (e) { 
        $('body').css('background','url(image/bg2.jpeg)');
    });
    $('#bg3').click(function (e) { 
        $('body').css('background','url(image/bg3.jpg)');
    });

})

function format(){
    var originalInput = $('.original-input').val();
    originalInput = originalInput.replace(/[\r\n]/g," ");
    $('.original-input').val(originalInput);
}


function translate(){
    var appid = "20211115000999543"
    var secret = "_fo4pd80BfK5QkhGevIk"
    var url = "https://fanyi-api.baidu.com/api/trans/vip/translate"
    var salt = (new Date).getTime();
    var query = $('.original-input').val();
    var from = 'auto';
    var value = $('#tolan').val()
    var to = value;
    var str1 = appid + query + salt +secret;
    var sign = MD5(str1);

    $.ajax({
        url: url,
        type: 'get',
        dataType: 'jsonp',
        data: {
            q: query,
            appid: appid,
            salt: salt,
            from: from,
            to: to,
            sign: sign
        },
        success: function (data) {
            $('.dest-input').val(data.trans_result[0].dst);
        } 
    });
}